package com.bstek.bdf2.core.view.auth;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.CoreHibernateDao;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.model.AuthApi;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
 

@Component("bdf2.roleAuthApiMaintain")
public class RoleAuthApiMaintain extends CoreHibernateDao{

	@DataProvider
	public Collection<AuthApi> loadAuthApi(String parentId, String roleId) throws Exception {
		List<AuthApi> result = new ArrayList<AuthApi>();
		String hql = "select u,(select count(id) from AuthApi child where child.parentId=u.id ) as child from "
				+ AuthApi.class.getName() + " u where 1=1 ";
		Map<String, Object> map = new HashMap<String, Object>();
		hql += " and u.roleId=:roleId  ";
		map.put("roleId", roleId);
		if (StringUtils.isEmpty(parentId)) {
			hql += " and u.parentId is null order by u.apiOrder asc ";
		} else {
			map.put("parentId", parentId);
			hql += " and u.parentId=:parentId order by u.apiOrder asc ";
		}
		List<Object[]> list = this.query(hql, map);
		for (Object[] obj : list) {
			AuthApi entity = EntityUtils.toEntity(obj[0]);
			//为了返回parentId等于当前id的所有数据的和。其目的是判断是否有子节点。
			EntityUtils.setValue(entity, "hasChild", ((Long) obj[1]).longValue() > 0);
			result.add(entity);
		}
		return result;
	}
	
	@Expose
	public int countChildren(String parentId) {
		String hql = "select count(id) from " + AuthApi.class.getName() + " d where d.parentId= :parentId";
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(parentId)) {
			parameterMap.put("parentId", parentId);
		}
		return this.queryForInt(hql, parameterMap);
	}
	
 
	@DataResolver
	@Transactional
	public void saveAuthApi(Collection<AuthApi> authApi){
		Session session = this.getSession();
		for (AuthApi parameter : authApi) {
			EntityState state = EntityUtils.getState(parameter);
			if (state.equals(EntityState.NEW)) {
				parameter.setId(UUID.randomUUID().toString().replaceAll("-", ""));
				parameter.setCreateTime(new Date());
				parameter.setCreateUser(ContextHolder.getLoginUserName());
 				session.save(parameter);
			} else if (state.equals(EntityState.MODIFIED) || state.equals(EntityState.MOVED)) {
				parameter.setCreateUser(ContextHolder.getLoginUserName());
				session.update(parameter);
			} else if (state.equals(EntityState.DELETED)) {
				session.delete(parameter);
			} else {
			}
			if (parameter.getChildren() != null) {
				saveAuthApi(parameter.getChildren());
			}

		}
	}
	
	
	 
}
